﻿using StudentManagement.Extensions.Pagenation;
using StudentManagement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using StudentManagement.Infrastructure.Repositories;
using System.Linq.Dynamic.Core;

namespace StudentManagement.Services
{
    public class CourseService : ICourseService
    {
        private readonly IRepository<Course> courseRepository;

        public CourseService(IRepository<Course> courseRepository)
        {
            this.courseRepository = courseRepository;
        }

        public PagedData<Course> GetPagedData(PageParameters pageParameters)
        {
            var data = courseRepository.GetAll();
            if (! String.IsNullOrEmpty(pageParameters.SortBy)) {
                data = data.OrderBy(pageParameters.SortBy);
            }
            if (! String.IsNullOrEmpty(pageParameters.SearchString)) {
                data = data.Where(c => c.Title.Contains(pageParameters.SearchString));
            }

            return new PagedData<Course>(data, pageParameters.PageIndex, pageParameters.PageSize);
        }
    }
}
